import { resolve } from 'path'
import path from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import dts from 'vite-plugin-dts'
// rollupOptions: {
//   output: {
//     manualChunks(id) {
//       if (id.includes("node_modules")) {
//         // 让每个插件都打包成独立的文件
//         return id.toString().split("node_modules/")[1].split("/")[0].toString();
//       }
//     }
//   }
// }
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue(), dts({ rollupTypes: true })],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'), // 配置 @ 指向 src 目录
    }
  },
  css: {
    preprocessorOptions: {
      scss: {
        charset: false
      }
    }
  },
  build: {
    rollupOptions: {
      external: ["vue", "echarts", "element-plus", "@element-plus/icons-vue", "axios", "@xterm/addon-fit", "@xterm/xterm"],
      output: {
        globals: {
          vue: 'vue',
          echarts: 'echarts',
          'element-plus': 'element-plus',
          axios: 'axios',
          "@element-plus/icons-vue": "@element-plus/icons-vue",
          "@xterm/xterm": "@xterm/xterm",
          "@xterm/addon-fit": "@xterm/addon-fit"
        },
      },
    },
    cssCodeSplit: false,
    minify: false,
    lib: {
      // Could also be a dictionary or array of multiple entry points
      // entry: resolve(__dirname, 'src/components/index.ts'),
      entry: resolve(__dirname, 'src/index.ts'),
      name: 'element-sci-ui',
      formats: ['es'],
      // the proper extensions will be added
      fileName: 'element-sci-ui'
    }
  },
  server: {
    host: '0.0.0.0',
    proxy: {
      '/api': {
        target: 'http://127.0.0.1:5000',
        changeOrigin: true,
        // rewrite: (path) => path.replace(/^\/api/, ''),
        ws: true
      }
    }
  },
})
